var num;//当前文本框的对象obj
var btn_num1;//正在显示的对象value（为计算“=”准备的）
var fh;//符号
var flage = false;
var data = {content: "", result: ""};//与后端对接对象

// 初始化
// TODO 暂时只支持顺序计算，根据符号优先级计算需要用到逆波兰算法
function init() {
    num = document.getElementById("num");
    // 文本框初始值为0
    num.value = "0";
    // 文本框不能输入
    num.disabled = "disabled";

    //点击数字，触发事件，把数字追加到文本框原来的内容，
    //更改为追加以后的数字
    //获取所有input的obj
    var oButton = document.getElementsByTagName("input");
    //遍历
    for (var i = 0; i < oButton.length; i++) {
        //如果点击触发事件，执行函数
        oButton[i].onclick = function () {
            //如果点击的是数字，就进行追加显示文本框的操作
            data.content = data.content + this.value;


            if (isNumber(this.value)) {
                // num.value = (num.value + this.value) * 1;
                //文本框的内容如果是空或者为0就更换为当前点击的value值，
                //不是（有数字）就追加到后面，并值给文本框的value
                if (flage) {
                    num.value = 0;
                    flage = false;
                }
                if (isNull(num.value)) {
                    num.value = this.value;
                } else {
                    num.value = num.value + this.value;
                }
            } else {
                //点击的是非数字
                //获取点击按钮的value值，并给变量
                var btn_num = this.value;

                switch (btn_num) {
                    case "+":
                        jisuan(num, btn_num);
                        break;
                    case "-":
                        jisuan(num, btn_num);
                        break;
                    case "*":
                        jisuan(num, btn_num);
                        break;
                    case "/":
                        jisuan(num, btn_num);
                        break;
                    case ".":
                        num.value = dec_numer(num.value);
                        data.content = dec_numer(data.content);
                        if (data.content == ".") {
                            data.content = "0" + ".";
                        }
                        if (!isNumber(data.content.charAt(data.content.length - 2))) {
                            data.content = data.content.substr(0, data.content.length - 1);
                            data.content = data.content + "0" + ".";
                        }
                        break;
                    case "←":
                        num.value = back(num.value);
                        n = data.content;
                        data.content = n.substr(0, n.length - 2);
                        break;
                    case "c":
                        num.value = "0";
                        btn_num1 = "";
                        fh = "";
                        data.content = "";
                        break;
                    case "+/-":
                        var n = num.value;
                        num.value = sign(num.value);
                        data.content = data.content.replace(n + '+/-', num.value);
                        break;
                    case "=":
                        flage = true;
                        compute(num.value);
                        data.result = num.value;
                        save(data);
                        data.content = "";
                        data.result = "";
                        break;
                    case "m":
                        break;
                }
            }
        }

    }

}

function compute(number) {
    if (!isNull(fh)) {
        switch (fh) {
            case "+":
                num.value = btn_num1 + Number(number);
                break;
            case "-":
                num.value = btn_num1 - Number(number);
                break;
            case "*":
                num.value = btn_num1 * Number(number);
                break;
            case "/":
                if (Number(number) == 0) {
                    alert("除数不能为0；");
                    num.value = 0;
                } else {
                    num.value = btn_num1 / Number(number);
                }
                break;
        }
        flage = true;

    }else {
        num.value = 0;
    }
}

//传来当前文件框的boj和点击的符号，把文件框的内容复制为btn_num1，
//并清空文件框,然后符号复制给ft
function jisuan(n, fh0) {
    var i = n.value;
    compute(num.value);
    fh = fh0;
    if(num.value == 0){
        btn_num1 = Number(i);
    }else{
        btn_num1 = Number(num.value);
    }
}

//正负号
function sign(n) {
    return Number(n) * -1;
}

//退位键,传入数字
function back(n) {
    n = n.substr(0, n.length - 1);
    if (isNull(n) || n == "-") {
        n = 0;
    }
    return n;
}

//小数点，传入数字
function dec_numer(n) {
    if (n.indexOf(".") == -1) {
        n = n + "."
    }
    return n;
}

// 验证文本框是否空或者0；
function isNull(n) {
    if (n == "" || n == null || n == undefined || n == "0" || n.length == 0) {
        return true;
    } else {
        return false;
    }
}

// 验证是否数字
function isNumber(n) {
    return !isNaN(n);
}



function save(data) {
    $.ajax({
        "url": "question/save",
        "type": "post",
        "dataType": "json",
        "data": JSON.stringify(data),
        "contentType": "application/json;charset=UTF-8",
        "success": function (response) {
            var result = response.code;
            if (result == "00000") {

            } else {
                alert(response.message)
            }
        }
    })
}
